﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RayTracer.Environment.Objects;
using RayTracer.Environment.Lights;
using Processing.Structures;
using System.Drawing;

namespace RayTracer
{
    /// <summary>
    /// Erweitert das Environment um Serialisierungsmethoden.
    /// </summary>
    static class EnvironmentScalarizeExtension
    {
        #region Other
        /// <summary>
        /// Gibt die Farbe in serialisierter Form zurück.
        /// </summary>
        /// <param name="color">Die Farbe.</param>
        /// <returns>Das Float4.</returns>
        public static Float4 Serialize(this Color color)
        {
            return new Float4(color.B / (float)255, color.G / (float)255, color.R / (float)255, color.A / (float)255);
        }

        /// <summary>
        /// Gibt die Größe in serialisierter Form zurück.
        /// </summary>
        /// <param name="size">Die Größe.</param>
        /// <returns>Der Int2.</returns>
        public static Int2 Serialize(this Size size)
        {
            return new Int2(size.Width, size.Height);
        }
        #endregion Other

        #region Objects
        /// <summary>
        /// Gibt den serialisierten Positionsarray der Objekte zurück.
        /// </summary>
        /// <param name="objects">Die Objekte.</param>
        /// <returns>Ein Float4-Array mit den Positionen.</returns>
        public static Float4[] GetSerializedPositions(this AObject[] objects)
        {
            Float4[] result = new Float4[objects.Length];
            for (int i = 0; i < objects.Length; i++) result[i] = objects[i].Position.Serialize();
            return result;
        }

        /// <summary>
        /// Gibt den serialisierten Rotationsarray der Objekte zurück.
        /// </summary>
        /// <param name="objects">Die Objekte.</param>
        /// <returns>Ein Float4-Array mit den Rotationen.</returns>
        public static Float4[] GetSerializedRotations(this AObject[] objects)
        {
            Float4[] result = new Float4[objects.Length];
            for (int i = 0; i < objects.Length; i++) result[i] = objects[i].Rotation.Serialize();
            return result;
        }

        /// <summary>
        /// Gibt den serialisierten Materialienarray der Objekte zurück.
        /// </summary>
        /// <param name="objects">Die Objekte.</param>
        /// <returns>Ein Float16-Array mit den Materialien.</returns>
        public static Float16[] GetSerializedMaterials(this AObject[] objects)
        {
            Float16[] result = new Float16[objects.Length];
            for (int i = 0; i < objects.Length; i++) result[i] = objects[i].Material.Serialize();
            return result;
        }

        #region Sphere
        /// <summary>
        /// Gibt den serialisierten Radiussarray der Kugeln zurück.
        /// </summary>
        /// <param name="spheres">Die Kugeln.</param>
        /// <returns>Ein float-Array mit den Radien.</returns>
        public static float[] GetSerializedRadiuses(this Sphere[] spheres)
        {
            float[] result = new float[spheres.Length];
            for (int i = 0; i < spheres.Length; i++) result[i] = spheres[i].Radius;
            return result;
        }
        #endregion Sphere
        #endregion Objects

        #region Lights
        /// <summary>
        /// Gibt den serialisierten Positionsarray der Lichter zurück.
        /// </summary>
        /// <param name="lights">Die Lichter.</param>
        /// <returns>Ein Float4-Array mit den Positionen.</returns>
        public static Float4[] GetSerializedPositions(this ALight[] lights)
        {
            Float4[] result = new Float4[lights.Length];
            for (int i = 0; i < lights.Length; i++) result[i] = lights[i].Position.Serialize();
            return result;
        }

        /// <summary>
        /// Gibt den serialisierten Farbenarray der  Lichter zurück.
        /// </summary>
        /// <param name="lights">Die Lichter.</param>
        /// <returns>Ein Float4-Array mit den Farben.</returns>
        public static Float4[] GetSerializedColors(this ALight[] lights)
        {
            Float4[] result = new Float4[lights.Length];
            for (int i = 0; i < lights.Length; i++) result[i] = lights[i].Color.Serialize();
            return result;
        }
        #endregion Lights
    }
}
